
package com.dog.basics.service.common.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dog.basics.bean.dto.admin.common.AdminDogNewsAddDto;
import com.dog.basics.bean.dto.admin.common.AdminDogNewsEditDto;
import com.dog.basics.bean.dto.admin.common.AdminDogNewsFromDto;
import com.dog.basics.bean.model.common.DogNews;
import com.dog.basics.bean.model.common.DogNewsRead;
import com.dog.basics.bean.vo.admin.common.AdminDogNewsVo;
import com.dog.basics.bean.vo.api.common.ApiDogNewsVo;
import com.dog.basics.common.util.PageParam;
import com.dog.basics.dao.common.DogNewsMapper;
import com.dog.basics.dao.common.DogNewsReadMapper;
import com.dog.basics.security.util.SecurityUtils;
import com.dog.basics.service.common.DogNewsService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 消息通知
 *
 * @author cy code generator
 * @date 2021-02-22 15:04:46
 */
@Service
@AllArgsConstructor
public class DogNewsServiceImpl extends ServiceImpl<DogNewsMapper, DogNews> implements DogNewsService {
    private final DogNewsReadMapper dogNewsReadMapper;

    /**
     * @Description: 分页
     * @Param: status
     * @return: IPage
     * @Author: cy
     * @Date:
     */
    @Override
    public IPage<AdminDogNewsVo> getAdminPageList(PageParam page, AdminDogNewsFromDto obj) {
        IPage iPage = this.page(page, Wrappers.<DogNews>lambdaQuery()
                .eq(ObjectUtil.isNotNull(obj.getNewsType()), DogNews::getNewsType, obj.getNewsType())
                .like(StrUtil.isNotBlank(obj.getTitle()), DogNews::getTitle, obj.getTitle())
                .orderBy(Boolean.TRUE, obj.getIsOrderByAvg(), DogNews::getCreateTime));
        List<DogNews> records = iPage.getRecords();
        iPage.setRecords(records.stream().map(e -> {
            AdminDogNewsVo newsVo = new AdminDogNewsVo();
            BeanUtil.copyProperties(e, newsVo);
            return newsVo;
        }).collect(Collectors.toList()));
        return iPage;
    }

    /**
     * @Description: api分页消息通知
     * @Param: status
     * @return: IPage<NewsVo>
     * @Author: wph
     * @Date:
     */
    @Override
    public IPage<ApiDogNewsVo> getApiPageList(PageParam page) {
        this.page(page, Wrappers.<DogNews>lambdaQuery().eq(DogNews::getNewsType, 0).or().eq(DogNews::getUserId, SecurityUtils.getUser().getUserId()).orderByDesc(DogNews::getCreateTime));
        List<DogNews> records = page.getRecords();
        page.setRecords(records.stream().map(e -> {
            ApiDogNewsVo apiNewsVo = new ApiDogNewsVo();
            BeanUtil.copyProperties(e, apiNewsVo);
            List<DogNewsRead> list = dogNewsReadMapper.selectList(Wrappers.<DogNewsRead>lambdaQuery().eq(DogNewsRead::getUserId, SecurityUtils.getUser().getUserId()).eq(DogNewsRead::getNewsId, e.getId()));
            apiNewsVo.setFlage(CollUtil.isNotEmpty(list) ? 0 : 1);
            return apiNewsVo;
        }).collect(Collectors.toList()));
        return page;
    }

    /**
     * @Description: 查询详情
     * @Param: obj
     * @return: Boolean
     * @Author: cy
     * @Date: 2020/05/19  0027
     */
    @Override
    public AdminDogNewsVo getAdminObjXq(Long id) {
        DogNews dogNews = this.getById(id);
        Validator.validateNotNull(dogNews, "common.noData");
        AdminDogNewsVo newsVo = new AdminDogNewsVo();
        BeanUtil.copyProperties(dogNews, newsVo);
        return newsVo;
    }

    /**
     * @Description: 查询详情APP
     * @Param: obj
     * @return: Boolean
     * @Author: cy
     * @Date: 2020/05/19  0027
     */
    @Override
    public ApiDogNewsVo getApiObjXq(Long id) {
        DogNews dogNews = this.getById(id);
        Validator.validateNotNull(dogNews, "common.noData");
        ApiDogNewsVo newsVo = new ApiDogNewsVo();
        BeanUtil.copyProperties(dogNews, newsVo);
        List<DogNewsRead> list = dogNewsReadMapper.selectList(Wrappers.<DogNewsRead>lambdaQuery().eq(DogNewsRead::getUserId, SecurityUtils.getUser().getUserId()).eq(DogNewsRead::getNewsId, dogNews.getId()));
        newsVo.setFlage(CollUtil.isNotEmpty(list) ? 0 : 1);
        return newsVo;
    }

    /**
     * @Description: 增加
     * @Param: obj
     * @return: Boolean
     * @Author: cy
     * @Date: 2020/05/19  0027
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveAdminObj(AdminDogNewsAddDto obj) {
        DogNews dogNews = new DogNews();
        BeanUtil.copyProperties(obj, dogNews);
        dogNews.setCreateTime(DateUtil.currentSeconds());
        dogNews.setCreateBy(SecurityUtils.getSysUser().getUserId());
        Validator.validateTrue(this.save(dogNews), "common.save");
        return Boolean.TRUE;
    }

    /**
     * @Description: 修改
     * @Param: obj
     * @return: Boolean
     * @Author: cy
     * @Date: 2020/05/19  0027
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean editAdminObj(AdminDogNewsEditDto obj) {
        DogNews dogNews = this.getById(obj.getId());
        Validator.validateNotNull(dogNews, "common.noData");
        BeanUtil.copyProperties(obj, dogNews);
        dogNews.setUpdateTime(DateUtil.currentSeconds());
        dogNews.setUpdateBy(SecurityUtils.getSysUser().getUserId());
        Validator.validateTrue(this.updateById(dogNews), "common.edit");
        return Boolean.TRUE;
    }

    /**
     * @Description: 删除
     * @Param: ids
     * @return: Boolean
     * @Author: cy
     * @Date: 2020/05/19  0027
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteObj(Long id) {
        DogNews dogNews = this.getById(id);
        Validator.validateNotNull(dogNews, "common.noData");
        Validator.validateTrue(this.removeById(id), "common.del");
        return Boolean.TRUE;
    }


    /**
     * @Description: 获取未读信息个数
     * @return: Integer
     * @Author: wph
     * @Date: 2020/05/19  0027
     */
    @Override
    public Integer getNum() {
        //获取已读消息的newIds
        List<Long> newIds = dogNewsReadMapper.selectList(Wrappers.<DogNewsRead>lambdaQuery().eq(DogNewsRead::getUserId, SecurityUtils.getUser().getUserId())).stream().map(e -> e.getNewsId()).collect(Collectors.toList());
        if (CollUtil.isEmpty(newIds)) {
            newIds.add(-2L);
        }
        Integer num = this.count(Wrappers.<DogNews>lambdaQuery().eq(DogNews::getNewsType, 0).or().eq(DogNews::getUserId, SecurityUtils.getUser().getUserId())) - this.count(Wrappers.<DogNews>lambdaQuery().in(CollUtil.isNotEmpty(newIds), DogNews::getId, newIds));
        return num;
    }

}
